<?PHP

class OP_PaginationWidget extends OP_WidgetTemplate{
	function render(){
		if (is_single()) return; // no pagination if single
		
		global $wpdb, $wp_query;
		$per_page = intval(get_query_var('posts_per_page')); // get number of posts per page, this is set in wordpress admin
		
		$paged = intval(get_query_var('paged')); // get paged var from url
		$current_page = !isset($paged) || $paged <= 1 ? 1 : $paged; // make sure current_page is set to 1 if not set

		$num = $wp_query->found_posts; // number of posts
		$max = $wp_query->max_num_pages; // max number
		
		$start_page = $current_page-4;
		$stop_page  = $current_page+4;

		// crop starting bounds
		if ($start_page < 1) {
			$stop_page += 1-$start_page;
			$start_page = 1;
		}
		
		// crop stopping bounds
		if ($stop_page > $max)
		{
			$start_page = max(1, $start_page-($stop_page-$max));
			$stop_page = $max;
		}
		
		// is there really anything to do?
		if ($stop_page == 1)
			// nothing to do, exit
			return;
		
		
		// previous link
		if($current_page != 1) 
			echo '<span class="pagination_nav"><a href="'.clean_url(get_pagenum_link($current_page - 1)).'">&laquo; Previous</a></span>';
		
		// Check for leading elipsis
		if ($start_page > 1)
		{
			$start_page += 1;
			echo '<span class="pagination_page"><a href="'.clean_url(get_pagenum_link(1)).'">1</a></span>';
			echo ' ... '; 
		}
		
		// loop through page numbers
		for($i=$start_page; $i <= $stop_page; $i++){
			if($i == $current_page){
				echo '<span class="pagination_page current">'.$i.'</span>'; // is current page
			}
			else{
				echo '<span class="pagination_page"><a href="'.clean_url(get_pagenum_link($i)).'">'.$i.'</a></span>'; // display link
			}
		}
		
		// Check for trailing elipsis
		if (($max-$stop_page) > 1)
		{
			$stop_page -= 1;
			echo ' ... ';
			echo '<span class="pagination_page"><a href="'.clean_url(get_pagenum_link($max)).'">'.$max.'</a></span>';
		}
		
		// next link
		if($current_page != $max) 
			echo '<span class="pagination_nav"><a href="'.clean_url(get_pagenum_link($current_page + 1)).'">Next &raquo;</a></span>';
		
		echo ' ('.$num .' total posts)'; // display total number of posts
		
		echo '<div class="forclear"></div>';
	}
	
	function do_post($post){
		$title = strip_tags(stripslashes($post["title"]));
		return compact( 'title' );		
	}

	function options(){
		$title = attribute_escape($this->values['title']);
		
		echo '<p><label for="'.$this->widget->class.'-title-'.$this->values['number'].'">'._e('Title:').' <input class="widefat" id="'.$this->widget->class.'-title-'.$this->values['number'].'" name="'.$this->widget->class.'['.$this->values['number'].'][title]" type="text" value="'.$title.'" /></label></p>';
	}	

	
}
		
$widget = new OP_Widget('op_pagination');
$widget->setMeta('OP Pagination',"Pagination navigation menu for your posts / pages");
$widget->setDefaults(array('title'=>''));

$opw_pagination = new OP_PaginationWidget($widget);
add_action('widgets_init', array($opw_pagination, 'init'));

?>